﻿/**
 * @file SDrag.js
 * @description
 *	simple drag effect
 * $id:v1.0.0, 2009-03-04 00:51, baboo<istxwang@gmail.com>$
 */
(function ($) {

$.SDrag = $({
	__init: function ($elem, sets) {
		$elem = typeof $elem == 'string' ? $($elem) : $elem;
		sets = $.extend({
			axis: 'ALL'	,	//ALL, V, H
			cursor: 'move',
			maxZIndex: 1000
		}, sets);
		
		var $drag = $elem;
		
		if (sets.$drag) 
			$drag = sets.$drag;
		else if ($elem.find('.sdrag-hand').length)
			$drag = $elem.find('.sdrag-hand');
		$drag.css({ cursor : sets.cursor });

		var position = $elem.position();
		if ($elem.css('position') != 'absolute') {
			$elem.css(position).css('position:absolute');	
		}
		if (sets.rangeX) {
			sets.rangeX[0] += position.left;
			sets.rangeX[1] += position.left;
		}
		if (sets.rangeY) {
			sets.rangeY[0] += position.top;
			sets.rangeY[1] += position.top;	
		}
		var axis = sets.axis.toUpperCase(), startCursorPointer, startPointer, oriZIndex;
		var start = function (event) {
			startCursorPointer = event.pointer();
			startPointer = { x : parseInt($elem.css('left')) || 0, y : parseInt($elem.css('top')) || 0 };
			oriZIndex = $elem.css('z-index') || 0;
			$elem.css('z-index:' + sets.maxZIndex);
			if (document.body.setCapture){
          				document.body.setCapture();
       			} else if (window.captureEvents){
          				window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
       			}
			$.bind(document, 'mousemove', move);
			$.bind(document, 'mouseup', stop);
			(sets.onStartDrag || $.nothing)($elem);
			event.stop();
		};
		var stop = function (event) {
			$elem.css('z-index:' + oriZIndex);
			if (document.body.releaseCapture) {
              				document.body.releaseCapture();
            			} else if (window.captureEvents) {
              				window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
            			}
			$.unbind(document,'mousemove', move);
			$.unbind(document, 'mouseup', stop);
			(sets.onEndDrag || $.nothing)($elem);
			event.stop();
		};
		var move = function (event) {
			var currentCursorPointer = event.pointer();
			var delta = { x : currentCursorPointer.x - startCursorPointer.x, y : currentCursorPointer.y - startCursorPointer.y };
			var p = {}, p1 = {};
			if (axis == 'ALL' || axis == 'H') {
				var left = startPointer.x + delta.x;
				if (sets.rangeX) {
					var rangeX = sets.rangeX;
					left = left < rangeX[0] ? rangeX[0] :  (left > rangeX[1] ? rangeX[1] : left);	
				}
				p1['left'] = left;
				p['left'] = left  + 'px';
			}
			if (axis == 'ALL' || axis == 'V') {
				var top = startPointer.y + delta.y;
				if (sets.rangeY) {
					var rangeY = sets.rangeY;
					top = top < rangeY[0] ? rangeY[0] : (top > rangeY[1] ? rangeY[1] : top);	
				}
				p1['top'] = top;
				p['top'] = top + 'px';
			}
			if (sets.onDrag) sets.onDrag(p1);
			$elem.css(p);
			event.stop();
		};
		var activated = false;
		this.activate = function () {
			if (activated) return;
			activated = true;
			$drag.mousedown(start);
		};
		this.disable = function () {
			if (!activated) return;
			activated = false;
			$drag.$mousedown(start);
		};
		this.activate();
	}
});

})($imple);